package com.tm.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.tm.dto.SetmaelDTO;

import com.tm.entity.CategoryEntity;
import com.tm.entity.DishEntity;
import com.tm.entity.SetmealEntity;
import com.tm.service.CategoryService;
import com.tm.service.DishService;
import com.tm.service.SetmaelService;
import com.tm.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @PROJECT_NAME: tm-wm-syb
 * @DESCRIPTION:
 * @USER: DELL
 * @DATE: 2023/4/9 15:43
 */
@Api(value = "套餐管理")
@RestController
@RequestMapping("setmeal")
public class SetmaelControlller {
    @Autowired
    private SetmaelService setmaelService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishService dishService;

    /**
     * 查询
     * @param page
     * @param pageSize
     * @param setmaelDTO
     * @return
     */
    @ApiOperation("查询套餐")
    @RequestMapping("page")
    public R page(@RequestParam(required = false, defaultValue = "1") Integer page,
                  @RequestParam(required = false, defaultValue = "10") Integer pageSize, SetmaelDTO setmaelDTO) {
        Page<SetmealEntity> setmaelEntityPage = setmaelService.pageList(page, pageSize, setmaelDTO);
        return R.success(setmaelEntityPage);

    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @ApiOperation("删除套餐")
    @DeleteMapping("deleteSeteal"+"{ids}")
    public R deleteSeteal(@PathVariable("ids") List<Long> ids) {
        setmaelService.removeByIds(ids);
        return R.success();
    }
    @PostMapping
    public R addsetmeal(@RequestBody SetmealEntity setmealEntity){
        Boolean fals  = setmaelService.addsetmeal(setmealEntity);
        return R.success(fals);
    }
    /**
     * 查询分类
     * @return
     */
    @GetMapping("/{id}")
    public R querySetmealById(@PathVariable Long id){
        DishEntity byId = dishService.getById(id);

        return R.success(byId);
    }
    @GetMapping("list")
    public R catlist(){
        List<CategoryEntity> list = categoryService.list();
        return R.success(list);
    }

    /**
     * 修改
     * @param setmealEntity
     * @return
     */
    @ApiOperation("修改套餐")
    @PutMapping
    public R editSetmeal(@RequestBody SetmealEntity setmealEntity){
        UpdateWrapper<SetmealEntity> updateWrapper =new UpdateWrapper<>();
        updateWrapper.eq("id",setmealEntity.getId());
        boolean update = setmaelService.update(setmealEntity, updateWrapper);
        return R.success(update);
    }
    @GetMapping("query/{id}")
    public R setmeallist(@PathVariable Long id){
        SetmealEntity byId = setmaelService.getById(id);
        return R.success(byId);
    }

    /**
     * 状态
     * @param ids
     * @return
     */
    @PostMapping("zhuangtai")
    public R zhuangtai(String ids){
        String[] split = ids.split(",");
        for (int i = 0; i < split.length; i++) {
            if(split[i]!=null && split[i]!=""){
                List<Long> collect = Arrays.stream(split).map(e -> Long.parseLong(e)).collect(Collectors.toList());
                return setmaelService.plqs(collect);
            }
        }
        return R.success();
    }

}
